package com.xindacloud.sms.console.system.service.impl;

import com.xindacloud.sms.console.result.Result;
import com.xindacloud.sms.console.result.ResultGenerator;
import com.xindacloud.sms.console.system.dto.SmsUiContentDTO;
import com.xindacloud.sms.console.system.entity.SmsUiContent;
import com.xindacloud.sms.console.system.mapper.SmsUiContentMapper;
import com.xindacloud.sms.console.system.service.ISmsUiContentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 系统主菜单表 服务实现类
 * </p>
 *
 * @author czc
 * @since 2019-10-08
 */
@Service
public class SmsUiContentServiceImpl extends ServiceImpl<SmsUiContentMapper, SmsUiContent> implements ISmsUiContentService {

	@Override
	public Result getSmsUiContentList(SmsUiContentDTO smsUiContentDTO) {
		IPage<SmsUiContent> page=new Page<>(smsUiContentDTO.getPageNum(),smsUiContentDTO.getPageSize());
		QueryWrapper<SmsUiContent> queryWrapper=new QueryWrapper<>();
		if(smsUiContentDTO.getTitleName()!=null) {
		queryWrapper.eq("title_name", smsUiContentDTO.getTitleName());
		}
		return ResultGenerator.genSuccessResult(this.page(page,queryWrapper));
	}

	@Override
	public Result saveOrUpdateSmsUiContent(SmsUiContentDTO smsUiContentDTO) {
		SmsUiContent smsUiContent=new SmsUiContent();
		try {
			BeanUtils.copyProperties(smsUiContentDTO,smsUiContent);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(this.saveOrUpdate(smsUiContent)){
		    return ResultGenerator.genSuccessResult("保存或修改成功！");
		}else {
			return ResultGenerator.genClientFailResult("保存或修改失败！");
		}
	}

	@Override
	public Result deleteSmsUiContent(SmsUiContentDTO smsUiContentDTO) {
		if(this.removeById(smsUiContentDTO.getId())){
		    return ResultGenerator.genSuccessResult("删除成功！");
		}else {
			return ResultGenerator.genClientFailResult("删除失败！");
		}
	}

	@Override
	public Result getSmsUiContentTreeList(SmsUiContentDTO smsUiContentDTO) {
		SmsUiContentDTO ssdto=new SmsUiContentDTO();
		ssdto.setId(0);
		getSystemMenus(ssdto);
		return ResultGenerator.genSuccessResult(ssdto.getChildren());
	}
	
	private SmsUiContentDTO getSystemMenus(SmsUiContentDTO menuDto) {
		List<SmsUiContentDTO> dtoList=new ArrayList<>();
		QueryWrapper<SmsUiContent> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("parent_id", menuDto.getId());
		List<SmsUiContent> menusList=this.list(queryWrapper);
		if(menusList!=null&& menusList.size()>0) {
		for(int i=0;i<menusList.size();i++) {
			SmsUiContentDTO dto2=new SmsUiContentDTO();
			BeanUtils.copyProperties(menusList.get(i),dto2);
			if(menuDto.getId()==0) {
				dto2.setParentId(null);
			}
			dtoList.add(dto2);
		}
		menuDto.setChildren(dtoList);
		}
		if(menusList!=null && menusList.size()>0) {
		getMenuList(menuDto);
		}
		return menuDto;
	}

	private void getMenuList(SmsUiContentDTO menuDto) {
		for(int i=0;i<menuDto.getChildren().size();i++) {
			getSystemMenus(menuDto.getChildren().get(i));
		}
	}

}
